﻿#region Disclaimer/Info
///////////////////////////////////////////////////////////////////////////////////////////////////
// MetaCollection
// 
// MetaCollection is a system to provide a standard structure, permissions and metadata service
// for content organization.
//
// This project is hosted at Google Code at http://code.google.com/p/metacollection/
//
// This project is licensed under the BSD license.  See the License.txt file for more information.
///////////////////////////////////////////////////////////////////////////////////////////////////
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using MetaCollection.Framework.Repository;

namespace MetaCollection.Framework.Tests.Fakes
{
	/// <summary>
	/// Mocks up a Resource Repository that uses an in memory collection for resource storage
	/// </summary>
	public class FakeResourceRepository : IResourceRepository
	{

		private Dictionary<string, string> m_Resources;

		public FakeResourceRepository()
		{
			m_Resources = new Dictionary<string, string>();
			m_Resources.Add("C11D81B4-524F-484b-9754-AD0E04691DD0-1", "ROOT RESOURCE v.1");
			m_Resources.Add("C11D81B4-524F-484b-9754-AD0E04691DD0-2", "ROOT RESOURCE v.2");
			m_Resources.Add("9AF786C6-E138-45d2-B150-E51C989EE3D0-1", "CHILD ONE RESOURCE");
			m_Resources.Add("EBD1B6C6-52F0-4dd0-AA6D-AC752E8BFD5C-1", "CHILD ONE CHILD DATA");
			m_Resources.Add("9AF786C6-E138-45d2-B150-E51C989EE3D0-1", "OTHER DATA");
		}

		public void Initialize(string configData) { }

		#region IResourceRepository Members

		/// <summary>
		/// Gets the resource stream.
		/// </summary>
		/// <param name="item">The item.</param>
		/// <returns></returns>
		public System.IO.Stream GetResourceStream(MetaCollection.Framework.Model.IItemInstance item)
		{
			string key = String.Format("{0}-{1}",item.Id.ToString("D"), item.Version);
			if (m_Resources.ContainsKey(key))
				return new MemoryStream(Encoding.UTF8.GetBytes(m_Resources[key]));
			else
				return null;
		}

		/// <summary>
		/// Saves the resource stream.
		/// </summary>
		/// <param name="item">The item.</param>
		/// <param name="data">The data.</param>
		public void SaveResourceStream(MetaCollection.Framework.Model.IItemInstance item, System.IO.Stream data)
		{
			string key = String.Format("{0}-{1}", item.Id.ToString("D"), item.Version);
			if (m_Resources.ContainsKey(key))
				m_Resources.Remove(key);
			StreamReader sr = new StreamReader(data);
			m_Resources.Add(key,sr.ReadToEnd());
		}

		#endregion
	}
}
